package com.ml.carManager.ui.login;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatEditText;

import com.hjq.toast.ToastUtils;
import com.ml.carManager.R;
import com.ml.carManager.ui.dialog.WaitDialog;
import com.ml.carManager.ui.diver.MainDivierActivity;
import com.ml.carManager.utils.auth.AuthManager;
import com.ml.carManager.utils.auth.callback.OnAuthListener;

import butterknife.BindView;
import custom.base.entity.auth.LoginUser;
import custom.base.entity.base.BaseResponse;
import custom.base.ui.activity.AppManager;
import custom.base.ui.activity.BaseActivity;
import custom.base.utils.TxtUtil;
import custom.captcha.widget.BlockPuzzleDialog;

public class LoginActivity extends BaseActivity {
    @BindView(R.id.act_login_username)
    AppCompatEditText etUsername;
    @BindView(R.id.act_login_password)
    AppCompatEditText etPassword;
    @BindView(R.id.act_login_enter)
    AppCompatButton btnEnter;
    @BindView(R.id.act_login_show_password)
    ImageView ivShowPassword;

    private WaitDialog waitDialog;
    private BlockPuzzleDialog blockPuzzleDialog;

    private boolean showPassword;

    @Override
    public int getLayoutID() {
        return R.layout.act_login;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public boolean initStatusBar() {
        return false;
    }

    @Override
    public void befordCreateView(@NonNull Bundle savedInstanceState) {
        super.befordCreateView(savedInstanceState);
    }

    @Override
    public void initView(@NonNull Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        waitDialog = new WaitDialog(this);
        waitDialog.setContent("正在登录");
        waitDialog.setCanceledOnTouchOutside(false);

        blockPuzzleDialog = new BlockPuzzleDialog(this);
    }

    @Override
    public void initObject(@NonNull Bundle savedInstanceState) {
        super.initObject(savedInstanceState);
    }

    @Override
    public void initData(@NonNull Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        etUsername.setText(AuthManager.getInstance(this).getAccount());
        etPassword.setText(AuthManager.getInstance(this).getAccountPassword());
    }

    @Override
    public void initListener() {
        ivShowPassword.setOnClickListener(this);
        blockPuzzleDialog.setOnResultsListener(new BlockPuzzleDialog.OnResultsListener() {
            @Override
            public void onResultsClick(String result) {

                waitDialog.show();
                AuthManager.getInstance(getActivity()).login(etUsername.getText().toString(),
                        etPassword.getText().toString(), result, getALiveNetCallback(), new OnAuthListener() {
                            @Override
                            public void onAuthSuccess(LoginUser sysUser) {
                                waitDialog.dismiss();
                                blockPuzzleDialog.dismiss();

                                AppManager.getInstance().finishAllActivity();

                                startActivity(MainDivierActivity.class);

                            }

                            @Override
                            public void onAuthFail(boolean isCodeError, BaseResponse<LoginUser> response, Throwable t) {
                                waitDialog.dismiss();
                                ToastUtils.show("登录失败" + t.getMessage());
                            }
                        });
            }
        });

        btnEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();
                if (TxtUtil.isEmpty(username)) {
                    ToastUtils.show("请填写手机号");
                    return;
                }
                if (TxtUtil.isEmpty(password)) {
                    ToastUtils.show("请填写密码");
                    return;
                }
                blockPuzzleDialog.show();
            }
        });
    }


    @SuppressLint("CheckResult")
    @Override
    public void onClick(View v, int clickID) {
        if (clickID == ivShowPassword.getId()) {
            if (showPassword) {
                showPassword = false;
                ivShowPassword.setSelected(false);
                //否则隐藏密码
                etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                etPassword.setSelection(etPassword.getText().toString().length());
            } else {
                showPassword = true;
                ivShowPassword.setSelected(true);
                //如果选中，显示密码
                etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                etPassword.setSelection(etPassword.getText().toString().length());
            }
        }
    }

    public void requestPermission() {
//        new RxPermissions(getActivity())
//                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
//                .subscribe(new Consumer<Boolean>() {
//                    @Override
//                    public void accept(@NonNull Boolean aBoolean) throws Exception {
//                        if (!aBoolean) {
//                            SimpleHintDialog simpleHintDialog = new SimpleHintDialog(getActivity());
//                            simpleHintDialog.setContentString("被禁止权限后无法正常使用软件");
//                            simpleHintDialog.setCancleBtnTxt("退出");
//                            simpleHintDialog.setEnterBtnTxt("重新获取");
//                            simpleHintDialog.setOnCancelClickListener(new OnCancelClickListener() {
//                                @Override
//                                public void onCancel() {
//                                    AppManager.getInstance().finishAllActivity();
//                                }
//                            });
//                            simpleHintDialog.setOnEnterClickListener(new OnEnterClickListener() {
//                                @Override
//                                public void onEnter() {
//                                    requestPermission();
//                                }
//                            });
//                            simpleHintDialog.show();
//                        } else {
//                        }
//                    }
//                });
    }


//    private long exitTime = 0;
//
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK
//                && event.getAction() == KeyEvent.ACTION_DOWN) {
//            if ((System.currentTimeMillis() - exitTime) > 2000) {
//                ToastUtils.show("再按一次退出程序");
//                exitTime = System.currentTimeMillis();
//            } else {
//                finish();
//            }
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
